package com.hetao.xx.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.hetao.xx.Global;
import com.hetao.xx.R;
import com.hetao.xx.adapter.NpcAdapter;
import com.hetao.xx.adapter.NpcEventAdapter;
import com.hetao.xx.adapter.PlaceAdapter;
import com.hetao.xx.game.common.EventEnum;
import com.hetao.xx.orm.NpcVo;
import com.hetao.xx.orm.NpcEventVo;
import com.hetao.xx.service.IntentManager;
import com.hetao.xx.service.PlaceEventService;
import com.hetao.xx.service.PlaceManager;
import com.hetao.xx.utils.UtilList;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HomeNpcFragment extends Fragment {

    private View root;
    private Unbinder unbinder;
    private NpcVo npc;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_info_layout)
    LinearLayout infoLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_event_layout)
    CardView eventLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_info)
    TextView infoView;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_event_list)
    ListView eventList;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_back)
    Button backButton;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_name)
    TextView userName;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_hp)
    TextView userHp;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_mp)
    TextView userMp;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_attack)
    TextView userAttack;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_defence)
    TextView userDefence;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_npc_speed)
    TextView userSpeed;

    NpcEventAdapter eventAdapter;

    private final Global global = Global.getInstant();
    private final PlaceManager placeManager = global.placeManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.fragment_home_npc, container, false);
            unbinder = ButterKnife.bind(this, root);
            init();
        }
        return root;
    }

    public void init() {
        eventAdapter = new NpcEventAdapter(getContext(), new ArrayList<>(4));
        eventList.setAdapter(eventAdapter);
        eventAdapter.setOnClickListener(v -> {
            NpcEventVo eventVo = (NpcEventVo) v.getTag();
            if (eventVo != null) {
                if (eventVo.eventType == EventEnum.Fight) {
                    global.fightManager.setEnemy(npc);
                    IntentManager.toFight(getContext());
                } else {
                    PlaceEventService.clickEvent(eventVo);
                }
            }
        });

    }

    public void update() {

        List<NpcEventVo> siteList = placeManager.getEventByNpcId(npc.id);
        if(UtilList.isEmpty(siteList)) {
            eventLayout.setVisibility(View.GONE);
        } else {
            eventLayout.setVisibility(View.VISIBLE);
            eventAdapter.setList(siteList);
            eventAdapter.notifyDataSetChanged();
        }
        infoLayout.setVisibility(View.GONE);
        userName.setText(npc.name);
        userHp.setText(String.valueOf(npc.Hp));
        userMp.setText(String.valueOf(npc.Mp));
        userAttack.setText(String.valueOf(npc.attack));
        userDefence.setText(String.valueOf(npc.defence));
        userSpeed.setText(String.valueOf(npc.speed));

    }

    @SuppressLint("NonConstantResourceId")
    @OnClick(R.id.home_npc_back)
    public void onClick(View view) {
        PlaceEventService.backNpc(npc);
    }

    public void setNpc(NpcVo npc) {
        this.npc = npc;
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //进行判空，避免空指针
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
